from models import Question, QuestionBank

def main():
    qb = QuestionBank('question_bank.db')

    while True:
        print("\nQuestion Bank Management")
        print("1. Add Question")
        print("2. Delete Question")
        print("3. Update Question")
        print("4. Display All Questions")
        print("5. Import Questions from CSV")
        print("6. Export Questions to CSV")
        print("7. Analyze Questions")
        print("8. Exit")

        choice = input("Enter your choice: ")

        if choice == '1':
            question = Question(
                course_id=int(input("Course ID: ")),
                knowledge_point_id=int(input("Knowledge Point ID: ")),
                question_type=input("Question Type (multiple choice/fill in the blank/true or false/subjective): "),
                content=input("Content: "),
                options=input("Options (separated by '|', leave empty if not applicable): ").split('|') if input("Options (separated by '|', leave empty if not applicable): ") else None,
                answer=input("Answer: "),
                explanation=input("Explanation: "),
                difficulty=float(input("Difficulty: "))
            )
            qb.add_question(question)

        elif choice == '2':
            try:
                question_id = int(input("Enter question ID to delete: "))
                qb.delete_question(question_id)
                print(f"Deleted question with ID {question_id}")
            except ValueError:
                print("Invalid input! Please enter a valid question ID.")

        elif choice == '3':
            try:
                question_id = int(input("Enter question ID to update: "))
                question = Question(
                    course_id=int(input("Course ID: ")),
                    knowledge_point_id=int(input("Knowledge Point ID: ")),
                    question_type=input("Question Type (multiple choice/fill in the blank/true or false/subjective): "),
                    content=input("Content: "),
                    options=input("Options (separated by '|', leave empty if not applicable): ").split('|') if input("Options (separated by '|', leave empty if not applicable): ") else None,
                    answer=input("Answer: "),
                    explanation=input("Explanation: "),
                    difficulty=float(input("Difficulty: "))
                )
                qb.update_question(question_id, question)
                print(f"Updated question with ID {question_id}")
            except ValueError:
                print("Invalid input! Please enter a valid question ID.")

        elif choice == '4':
            questions = qb.get_all_questions()
            for question in questions:
                print(question)

        elif choice == '5':
            filepath = input("Enter CSV file path to import: ")
            qb.import_questions_from_csv(filepath)
            print("Questions imported from CSV")

        elif choice == '6':
            filepath = input("Enter CSV file path to export: ")
            qb.export_questions_to_csv(filepath)
            print("Questions exported to CSV")

        elif choice == '7':
            analysis = qb.analyze_questions()
            print("Question Analysis:")
            print("Courses:")
            for course_id, count in analysis['course_counts']:
                print(f"Course ID {course_id}: {count} questions")
            print("Knowledge Points:")
            for kp_id, count in analysis['knowledge_point_counts']:
                print(f"Knowledge Point ID {kp_id}: {count} questions")
            print("Question Types:")
            for qt, count in analysis['question_type_counts']:
                print(f"{qt}: {count} questions")

        elif choice == '8':
            break

        else:
            print("Invalid choice, please try again.")

if __name__ == '__main__':
    main()
